package com.cc.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.mall.entity.OnlineOrderEntity;
import com.cc.mall.feign.UserFeign;
import com.cc.mall.vo.OrderDetail;
import com.cc.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cc.mall.dao.OfflineOrderDao;
import com.cc.mall.entity.OfflineOrderEntity;
import com.cc.mall.service.OfflineOrderService;
import org.springframework.util.StringUtils;


@Service("offlineOrderService")
public class OfflineOrderServiceImpl extends ServiceImpl<OfflineOrderDao, OfflineOrderEntity> implements OfflineOrderService {

    @Autowired
    private UserFeign userFeign;

    @Override
    public List<OrderDetail> getOnlineOrderDetailByOrderId(Long orderId) {
        return baseMapper.getOnlineOrderDetailByOrderId(orderId);
    }

    @Override
    public Page<OfflineOrderEntity> getOfflineOrderList(Map<String, Object> params) {
        String orderId = (String)  params.get("orderId");
        Long page = Long.parseLong((String) params.get("page"));
        Long limit = Long.parseLong((String) params.get("limit"));
        Page<OfflineOrderEntity> offlineOrderEntityPage = new Page<>(page,limit);
        String mobile = (String) params.get("mobile");
        String nickname = (String) params.get("nickname");
        QueryWrapper<OfflineOrderEntity> queryWrapper = new QueryWrapper<>();
        if(!(StringUtils.isEmpty(mobile) && StringUtils.isEmpty(nickname))) {
            UserEntity user = userFeign.getUserByMobileOrNickname(mobile, nickname);
            if(user != null){
                if(user.getPermission() == 1){
                    // 员工
                    queryWrapper.eq("eid",user.getUid());
                }else {
                    // 客户
                    queryWrapper.eq("cid",user.getUid());
                }
            }else {
                return offlineOrderEntityPage;
            }
        }
        if(!StringUtils.isEmpty(orderId)){
            int length = orderId.split("").length;
            if(length <=19){
                queryWrapper.eq("id",Long.parseLong(orderId));
            }else {
                return offlineOrderEntityPage;
            }
            queryWrapper.eq("id",Long.parseLong(orderId));
        }
        baseMapper.selectMapsPage(offlineOrderEntityPage,queryWrapper);
        return offlineOrderEntityPage;
    }
}